package com.lin.filegraph.service.impl;

import com.lin.filegraph.service.IStartRecovery;
import com.lin.filegraph.utils.extractor.JavaInfoExtractor;
import com.lin.filegraph.utils.filedg.FileGraph;
import com.lin.filegraph.utils.moduledg.ModuleInfo;
import com.lin.filegraph.utils.parser.astutil.ParsePom;
import com.lin.filegraph.utils.parser.node.JProjectNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class StartRecoveryImpl implements IStartRecovery {
    @Resource
    StartFileGraphImpl startFileGraph;

    public void StartRecovery() {
        try {
            //拉取代码
            System.out.println("开始拉取代码");

            System.out.println("拉取代码完毕");

            String projectPath = "D:\\研二下\\找实习\\开放API";
            //获取规则
            List<String> rules = new ArrayList<>();
            rules.add("经典聚类");
            rules.add("111111111");
            boolean filerTest = false;

            String fileSize = "0";
            for (int i = 2; i < rules.size() - 1; i += 2) {
                if (Objects.equals(rules.get(i), "文件大小")) {
                    fileSize = rules.get(i + 1);
                }
                if (Objects.equals(rules.get(i), "过滤测试文件")) {
                    filerTest = true;
                }
            }

            int loc = Integer.parseInt(fileSize);
            String method = rules.get(0);
            String depend = rules.get(1);
            if (Objects.equals(method, "经典聚类")) {
                //生成、存储文件依赖图
                long file_graph_start_time = System.currentTimeMillis();

                JProjectNode root = JavaInfoExtractor.getJProjectNode(projectPath);

//                System.out.println(JSONUtil.toJsonStr(root));

                FileGraph fileGraph = startFileGraph.createFileGraph(projectPath, root, depend, loc, filerTest, null);

//                System.out.println(JSONUtil.toJsonStr(fileGraph));

                //文件依赖图模块完毕
                long file_graph_end_time = System.currentTimeMillis();
                System.out.println("耗时："+(file_graph_end_time-file_graph_start_time)/1000+"s");
            } else {
                //生成、存储文件全量依赖图
                Date file_graph_start_time = new Date();

                JProjectNode root = JavaInfoExtractor.getJProjectNode(projectPath);

                List<ModuleInfo> moduleInfoList = new ArrayList<>();
                if (method.equals("Maven多模块")) {
                    moduleInfoList = ParsePom.getModule(projectPath);
                } else {
                    moduleInfoList = null;
                }

                FileGraph fileGraph = startFileGraph.createFileGraph(projectPath, root, depend, loc, filerTest, moduleInfoList);
            }
            //架构恢复结束

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
